import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";

export namespace Product {
  export interface ProductListItem {
    id: string;
    productCode: string;
    productName: string;
    model: string;
    category: string;
    type: string;
    specification: string;
    version: string;
    unit: string;
    status: string;
    routeId?: number;
    bom?: Bom; // BOM对象（通过单独接口获取）
    createdBy: string;
    createdTime: string;
    updatedBy: string;
    updatedTime: string;
    deleted: number;
  }

  export interface BomItem {
    materialCode: string;
    materialName: string;
    quantity: number;
    unit: string;
    remark?: string;
    children?: BomItem[];
  }

  export interface Bom {
    bomVersion: string;
    bomType: string;
    status: string;
    source: string;
    effectiveDate?: string;
    expiredDate?: string;
    items: BomItem[];
  }

  export interface CreateProductReq {
    productCode: string;
    productName: string;
    model: string;
    category: string;
    type: string;
    specification: string;
    version: string;
    unit: string;
    bom?: Bom;
  }

  export interface UpdateProductReq {
    productName: string;
    model: string;
    category: string;
    type: string;
    specification: string;
    version: string;
    unit: string;
    bom?: Bom;
  }

  export interface ChangeProductVersionReq {
    newVersion: string;
  }

  export interface AssignProductRouteReq {
    routeId: number;
  }

  export interface RejectProductReq {
    reason: string;
  }
}

/**
 * @name 产品管理模块
 */
// 获取产品列表
export const getProductList = (params: any) => {
  return http.get<any>(`/products`, params);
};

// 根据ID获取产品详情
export const getProductById = (id: number) => {
  return http.get<Product.ProductListItem>(`/products/${id}`);
};

// 根据状态获取产品列表
export const getProductByStatus = (params: any, status: string) => {
  return http.get<any>(`/products/status/${status}`, params);
};

// 创建产品
export const createProduct = (params: Product.CreateProductReq) => {
  return http.post<number>(`/products`, params);
};

// 更新产品
export const updateProduct = (id: number, params: Product.UpdateProductReq) => {
  return http.put(`/products/${id}`, params);
};

// 启用产品
export const enableProduct = (id: number) => {
  return http.post(`/products/${id}/enable`);
};

// 禁用产品
export const disableProduct = (id: number) => {
  return http.post(`/products/${id}/disable`);
};

// 删除产品
export const deleteProduct = (id: number) => {
  return http.delete(`/products/${id}`);
};

// 变更产品版本
export const changeProductVersion = (id: number, params: Product.ChangeProductVersionReq) => {
  return http.post(`/products/${id}/change-version`, params);
};

// 更新产品BOM
export const updateProductBOM = (id: number) => {
  return http.post(`/products/${id}/update-bom`);
};

// 分配工艺路线
export const assignProductRoute = (id: number, params: Product.AssignProductRouteReq) => {
  return http.post(`/products/${id}/assign-route`, params);
};

// 解绑工艺路线
export const unassignProductRoute = (productId: number, routeId: number) => {
  return http.delete(`/products/${productId}/routes/${routeId}`);
};

// 审核通过
export const approveProduct = (id: number) => {
  return http.post(`/products/${id}/approve`);
};

// 审核驳回
export const rejectProduct = (id: number, params: Product.RejectProductReq) => {
  return http.post(`/products/${id}/reject`, params);
};

// 获取产品BOM信息
export const getProductBOM = (productId: number) => {
  return http.get<Product.Bom>(`/products/${productId}/bom`);
};

